<?php

function getProprioGoods($BDD_connexion) {
	
	$goods = array();
	
	$request = "SELECT * FROM bien,adresse WHERE bien.ref_proprio = :id AND bien.ref_adresse = adresse.id_adresse GROUP BY adresse.id_adresse";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$_SESSION['userid'],PDO::PARAM_INT);
	$prepare->execute();
	while ($good = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$good->description = getLocationDescription($BDD_connexion,$good->id_bien);
		$goods[] = $good;
	}
	
	return $goods;
	
}

function updateGoodDescription($BDD_connexion,$id_bien,$id_langue,$content)
{
	
	$request = "UPDATE descriptions SET content = :content WHERE id_langue = :id_langue AND id_bien = :id_bien ";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindParam(':content',nl2br($content),PDO::PARAM_STR);
	$prepare->bindValue(':id_langue',$id_langue,PDO::PARAM_INT);
	$prepare->bindValue(':id_bien',$id_bien,PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function addGoodDescription($BDD_connexion,$id_bien,$id_langue,$content)
{
	
	$request = "INSERT INTO descriptions(id_bien,id_langue,content) VALUE (:id_bien,:id_langue,:content)";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindParam(':content',nl2br($content),PDO::PARAM_STR);
	$prepare->bindValue(':id_langue',$id_langue,PDO::PARAM_INT);
	$prepare->bindValue(':id_bien',$id_bien,PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function getGoodAddress($BDD_connexion,$id_bien)
{
	
	$request = "SELECT * FROM adresse WHERE id_adresse = (SELECT ref_adresse FROM bien WHERE bien.id_bien = :id_bien)";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(":id_bien",$id_bien,PDO::PARAM_INT);
	$prepare->execute();
	$address = $prepare->fetch(PDO::FETCH_OBJ);
	
	return $address;
	
}

function getGoodDescriptions($BDD_connexion,$id_bien)
{
	
	$descriptions = array();
	
	$request = "SELECT * FROM descriptions WHERE id_bien = :id_bien";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(":id_bien",$id_bien,PDO::PARAM_INT);
	$prepare->execute();
	while ($description = $prepare->fetch(PDO::FETCH_OBJ))
	{
		if ($description->id_langue == "1")
		{
			$descriptions["french"] = $description->content;
		}
		else if ($description->id_langue == "2")
		{
			$descriptions["english"] = $description->content;
		}
	}
	
	return $descriptions;
	
}

function deleteGood($BDD_connexion,$id_bien) {

	$request = "DELETE FROM bien WHERE id_bien = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(":id",$id_bien,PDO::PARAM_INT);
	
	return $prepare->execute();

}

function isOwnedByUser($BDD_connexion,$id)
{
	
	$user = $_SESSION['userid'];
	
	$request = "SELECT * FROM bien WHERE ref_proprio = :user AND id_bien = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(":user",$user,PDO::PARAM_INT);
	$prepare->bindValue(":id",$id,PDO::PARAM_INT);
	$prepare->execute();
	$boolean = $prepare->fetch(PDO::FETCH_OBJ);
	
	return isset($boolean->id_bien);
	
}

function isGoodIntermediaire($intermediaires,$id)
{
	
	foreach ($intermediaires as $intermed)
	{
		
		if ($intermed->id == $id)
		{
			return true;
		}
		
	}
	
	return false;
	
	
}

function getIntermediairesInfos($BDD_connexion,$goods)
{
	
	$intermediaires = array();
	
	if (!empty($goods))
	{
		
		$request = "SELECT * FROM personne WHERE id IN (:userId,";
		
		foreach ($goods as $good)
		{
			
			$request .= $good->ref_intermediaire.",";
			
		}
		
		$request = substr($request,0,-1).")";
		
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindValue(':userId',$_SESSION['userid'],PDO::PARAM_INT);
		$prepare->execute();
		while ($intermediaire = $prepare->fetch(PDO::FETCH_OBJ))
		{
			$intermediaires[$intermediaire->id] = $intermediaire;
		}
	
	}
	
	return $intermediaires;
	
	
	
}

function getIntermediaireOrClientOrOwnerByName($BDD_connexion, $login)
{

	$loginMaxLength = 255;
	$id = -1;
	
	$request = "SELECT id FROM personne WHERE login = :login";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindParam(":login", $login, PDO::PARAM_STR, $loginMaxLength);
	$prepare->execute();
	
	$idresult = $prepare->fetch(PDO::FETCH_OBJ);
	
	if (isset($idresult->id))
		$id = $idresult->id;
	{
	}
	
	return $id;
	
}

function promoteClientToIntermed($BDD_connexion, $userId)
{
	$request = "UPDATE personne SET ref_categorie= :cat WHERE id = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(":cat", 2, PDO::PARAM_INT);
	$prepare->bindValue(":id", $userId, PDO::PARAM_INT);
	
	return $prepare->execute();
	
}
	
function setIntermedToGood($BDD_connexion, $userId, $goodId)
{
	
	$request = "UPDATE bien SET ref_intermediaire= :user WHERE id_bien = :bien";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(":user", $userId, PDO::PARAM_INT);
	$prepare->bindValue(":bien", $goodId, PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function moveIntToInt($BDD_connexion, $oldId, $newId)
{
	
	$request = "UPDATE bien SET ref_intermediaire= :newId WHERE ref_intermediaire = :oldId AND ref_proprio = :proprio";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(":newId", $newId, PDO::PARAM_INT);
	$prepare->bindValue(":oldId", $oldId, PDO::PARAM_INT);
	$prepare->bindValue(":proprio",$_SESSION['userid'],PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function getIntermediaireGoods($BDD_connexion) {
	
	$goods = array();
	
	$request = "SELECT * FROM bien,adresse WHERE bien.ref_intermediaire = :id AND bien.ref_adresse = adresse.id_adresse GROUP BY adresse.id_adresse";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$_SESSION['userid'],PDO::PARAM_INT);
	$prepare->execute();
	while ($good = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$good->state = isNowBooked($BDD_connexion,$good->id_bien);
		$good->description = getLocationDescription($BDD_connexion,$good->id_bien);
		$goods[] = $good;
	}
	
	return $goods;
	
}

function getIntermediaire($BDD_connexion,$id) {
	
	$request = "SELECT id FROM personne, bien WHERE bien.ref_intermediaire = personne.id AND bien.id_bien = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	$intermediaire = $prepare->fetch(PDO::FETCH_OBJ);
	
	return $intermediaire;
	
}

?>

